package org.example.springsecurity_.controller;

import io.jsonwebtoken.JwtBuilder;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import io.jsonwebtoken.security.Keys;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.security.Key;
import java.util.Date;
import java.util.HashMap;

@RestController
public class HelloController {
    @RequestMapping("/hello")
    public String hello(){
        //令牌-JWT生成令牌
        Key key= Keys.secretKeyFor(SignatureAlgorithm.HS256);
        HashMap<String, Object> map=new HashMap<>();
        map.put("username","abc");

        JwtBuilder jwtBuilder = Jwts.builder()
                .setClaims(map)//声明
                .setIssuedAt(new Date())//颁发时间
                .setSubject("sbszt")
                .signWith(key);//签名的key
        String token=jwtBuilder.compact();//JWT
        return token;
    }
}
